package com.dycx.system.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.BASE64DecodedMultipartFile;
import com.dycx.common.Common;
import com.dycx.common.dto.FileDTO;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.system.entity.OssFileDO;
import com.dycx.system.mapper.OssFileMapper;
import com.dycx.system.service.OssFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件管理(OssFile)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-21 14:40:57
 */

@Service
public class OssFileServiceImpl extends ServiceImpl<OssFileMapper, OssFileDO> implements OssFileService {
    /**
     * 新增数据
     *
     * @param src 实例对象
     * @return 实例对象
     */

    @Override
    public void add(String src) {
        if (StringUtils.isBlank(src)) return;
        LambdaQueryWrapper<OssFileDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OssFileDO::getSrc, src);
        wrapper.last("limit 1");
        if (count(wrapper) > 0) return;

        OssFileDO entity = new OssFileDO();
        entity.setSrc(src);
        entity.setSum(0);

        this.save(entity);
    }

    @Override
    public void add(List<FileDTO> list) {
        if (ObjectUtil.isEmpty(list)) return;
        list.forEach(item -> {
            add(item.getUrl());
        });
    }


    @Override
    public void setUse(String src) {
        edit(src, 1);
    }

    @Override
    public void setUse(String src, Boolean isHtml) {
        if (isHtml) {
            setHtmlFileStatus(src, true);
        } else {
            edit(src, 1);
        }
    }

    @Override
    public void setUse(List<FileDTO> list) {
        if (ObjectUtil.isEmpty(list)) return;
        list.forEach(item -> {
            edit(item.getUrl(), 1);
        });
    }

    @Override
    public void setDeleted(String src) {
        edit(src, -1);
    }

    @Override
    public void setDeleted(String src, Boolean isHtml) {
        if (isHtml) {
            setHtmlFileStatus(src, false);
        } else {
            edit(src, -1);
        }
    }

    @Override
    public void setDeleted(List<FileDTO> list) {
        if (ObjectUtil.isEmpty(list)) return;
        list.forEach(item -> {
            edit(item.getUrl(), -1);
        });
    }

    private void setHtmlFileStatus(String html, Boolean use) {
        if (StrUtil.isBlank(html)) return;

        List<String> urls = Common.getImageUrl(html);
        if (urls.size() == 0) return;

        urls.forEach(url -> {
            edit(url, use ? 1 : -1);
        });
    }

    /**
     * 修改数据
     *
     * @param src
     * @param plus
     * @return
     */
    private void edit(String src, int plus) {
        if (StrUtil.isBlank(src)) return;
        src = AliyunOss.delDomain(src);

        LambdaQueryWrapper<OssFileDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OssFileDO::getSrc, src);
        wrapper.last("limit 1");

        OssFileDO entity = getOne(wrapper);
        if (ObjectUtil.isNull(entity)) return;
        entity.setSum(entity.getSum() + plus);

        updateById(entity);
    }

    /**
     * 匹配base图片，并上传替换
     *
     * @param html
     * @return
     */
    @Override
    public String regexBase64Upload(String html) {
        if (StringUtils.isBlank(html)) return "";
        List<String> imgUrlList = Common.getImageUrl(html);

        for (String url : imgUrlList) {
            if (StringUtils.contains(html, "base64")) {
                String newUrl = AliyunOss.uploadStream(BASE64DecodedMultipartFile.base64ToMultipart(url));
                this.add(newUrl);
                html = html.replace(url, newUrl);
            }
        }

        return html;
    }
}

